首页

欢迎

 

Welcome

欢迎, 这是一个学习数学、讨论数学的网站.

转到问题

请输入问题号, 例如: 2512

IMAGINE, THINK, and DO
How to be a scientist, mathematician and an engineer, all in one?
--- S. Muthu Muthukrishnan

Local Notes

Local Notes 是一款 Windows 下的笔记系统.

Local Notes 下载

Sowya

Sowya 是一款运行于 Windows 下的计算软件.

详情

下载 Sowya.7z (包含最新版的 Sowya.exe and SowyaApp.exe)


注: 自 v0.550 开始, Calculator 更名为 Sowya. [Sowya] 是吴语中数学的发音, 可在 cn.bing.com/translator 中输入 Sowya, 听其英语发音或法语发音.





注册

欢迎注册, 您的参与将会促进数学交流. 注册

在注册之前, 或许您想先试用一下. 测试帐号: usertest 密码: usertest. 请不要更改密码.


我制作的 slides

Problem

随机显示问题

Problèmes d'affichage aléatoires

软件 >> C++
Questions in category: C++ (C++).

error C2011: “sockaddr”:“struct”类型重定义

Posted by haifeng on 2025-11-09 20:59:36 last update 2025-11-09 22:18:27 | Answers (0)


error C2011: “sockaddr”:“struct”类型重定义

这个问题的出现主要伴随着包含 Windows.h 的文件. 打开 Windows.h 这个文件. 有这样一段.


#ifndef WIN32_LEAN_AND_MEAN
#include <cderr.h>
#include <dde.h>
#include <ddeml.h>
#include <dlgs.h>
#ifndef _MAC
#include <lzexpand.h>
#include <mmsystem.h>
#include <nb30.h>
#include <rpc.h>
#endif
#include <shellapi.h>
#ifndef _MAC
#include <winperf.h>
#include <winsock.h>
#endif
#ifndef NOCRYPT
#include <wincrypt.h>
#include <winefs.h>
#include <winscard.h>
#endif

#ifndef NOGDI
#ifndef _MAC
#include <winspool.h>
#ifdef INC_OLE1
#include <ole.h>
#else
#include <ole2.h>
#endif /* !INC_OLE1 */
#endif /* !MAC */
#include <commdlg.h>
#endif /* !NOGDI */
#endif /* WIN32_LEAN_AND_MEAN */


我们看到如果没有定义 WIN32_LEAN_AND_MEAN, 且也没有定义 _MAC, 则会加载 winsock.h

而 winsock.h 中有 sockaddr 结构的定义:

/*
 * Structure used by kernel to store most
 * addresses.
 */
struct sockaddr {
        u_short sa_family;              /* address family */
        char    sa_data[14];            /* up to 14 bytes of direct address */
};


而 ws2def.h 中也有 sockaddr 结构的定义

//
// Structure used to store most addresses.
//
typedef struct sockaddr {

#if (_WIN32_WINNT < 0x0600)
    u_short sa_family;
#else
    ADDRESS_FAMILY sa_family;           // Address family.
#endif //(_WIN32_WINNT < 0x0600)

    CHAR sa_data[14];                   // Up to 14 bytes of direct address.
} SOCKADDR, *PSOCKADDR, FAR *LPSOCKADDR;


WinSock2.h 在必要情况下会包含进 Windows.h

/*
 * Pull in WINDOWS.H if necessary
 */
#ifndef _INC_WINDOWS
#include <windows.h>
#endif /* _INC_WINDOWS */

此外它引入了 ws2def.h, 

#include <ws2def.h>

 

因此, 解决冲突的办法是仅使用其中一种定义. 一般现在不使用 winsock.h. 故在项目中凡是 #include <Windows.h> 的地方, 在前面加上 #define WIN32_LEAN_AND_MEAN

 


注: 以我的电脑为例,

Windows.h 所在文件夹一般为 C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um

ws2def.h 所在文件夹为 C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\shared

winsock.h 和 WinSock2.h 均位于C:\Program Files (x86)\Windows Kits\10\Include\10.0.26100.0\um